package com.yiqiu.system.service.impl;

import com.yiqiu.common.core.domain.entity.SysUser;
import com.yiqiu.system.mapper.SysUserMapper;
import com.yiqiu.system.service.SysUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
/**
 * @author weiyi
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    private SysUserMapper sysUserMapper;

    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public SysUser selectUserByUsername(String username) {
        // 根据用户名查询用户
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name", username));
    }

    @Override
    public SysUser selectUserById(Long userId) {
        // 根据用户ID查询用户
        return sysUserMapper.selectById(userId);
    }

    @Override
    public void insertUser(SysUser user) {
        // 插入新用户
        sysUserMapper.insert(user);
    }

    @Override
    public void updateUser(SysUser user) {
        // 更新用户信息
        sysUserMapper.updateById(user);
    }

    @Override
    public void deleteUserById(Long userId) {
        // 根据用户ID删除用户
        sysUserMapper.deleteById(userId);
    }

    @Override
    public List<SysUser> selectUserList(SysUser user) {
        // 根据条件查询用户列表
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            if (user.getUserName() != null) {
                queryWrapper.like("user_name", user.getUserName());
            }
            if (user.getStatus() != null) {
                queryWrapper.eq("status", user.getStatus());
            }
            if (user.getPhoneNumber() != null) {
                queryWrapper.like("phone_number", user.getPhoneNumber());
            }
        }
        return sysUserMapper.selectList(queryWrapper);
    }
}
